import { Command } from "commander";
import { CreateBridge } from "./CreateBridge";
import * as fs from "fs";
import { XChainBridge } from "xrpl/dist/npm/models/common";

export type BridgeConfig = {
    lockingChain: {
        type: "xrp" | "evm";
        url: string;
        tokenCode: string;
        tokenIssuer: string;
        fundingPrivateKey: string;
        witnesses: string[];
        XChainBridge?: XChainBridge;
    };
    issuingChain: {
        type: "xrp" | "evm";
        url: string;
        fundingPrivateKey: string;
        witnesses: string[];
        XChainBridge?: XChainBridge;
    };
    threshold: number;
    minRewardAmount: number;
    minCreateAmount: number;
};

const parse = (args: any): BridgeConfig | undefined => {
    if (args.config) {
        return JSON.parse(fs.readFileSync(args.config).toString());
    }
    return undefined;
};

const program = new Command("create")
    .option("-c, --config <value>", "The path of the config json file")
    .option("-e, --export <value>", "The path to export the config json file")
    .addHelpText(
        "afterAll",
        `Config file must have the following format: 
    ${JSON.stringify(
        {
            lockingChain: {
                type: "evm",
                url: "https://rpc-evm-sidechain.peersyst.tech",
                tokenCode: "USD",
                tokenIssuer: "0x92AE4ba2305F6c64E9715b60CF774784079C463B",
                fundingPrivateKey: "****",
                witnesses: ["0x96329A50d10a3F69311E4f4E108672926c51c474"],
            },
            issuingChain: {
                type: "xrp",
                url: "wss://sidechain-net1.devnet.rippletest.net",
                fundingPrivateKey: "****",
                witnesses: ["rpSspP5yYyomcSrgsohyKMCnu5oJsTMkYP"],
            },
            threshold: 1,
            minRewardAmount: 1,
            minCreateAmount: 20,
        },
        undefined,
        4,
    )}`,
    )
    .action(async (args) => {
        const parsedArgs = parse(args);
        if (parsedArgs) {
            await CreateBridge(parsedArgs, args.export);
            process.exit(0);
        } else {
            program.help();
            process.exit(1);
        }
    });
export default program;
